//
//  BoxFamilyMangerViewController.m
//  workApp
//
//  Created by 林 on 2019/1/12.
//  Copyright © 2019 林. All rights reserved.

#import "BoxFamilyMangerViewController.h"
#import "BoxMangerCell/BoxMangerTableViewCell.h"
#import "BandingBtn.h"
@interface BoxFamilyMangerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * tableV;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,strong)UIView * backView;
@property (nonatomic,strong)UIView * backViewOne;
@property (nonatomic,strong)NSMutableArray * addArray;
@property (nonatomic,strong)NSMutableArray * delArray;
@end

@implementation BoxFamilyMangerViewController
- (void)getData{
    NSDictionary * dic = @{@"family_id":FamilyID};
    [MDNetDetail GETNet:FamilyMenber_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
//        NSLog(@"%@",posts);
        NSArray * array = posts[@"data"];
        NSLog(@"%@",array);
        for (NSDictionary * dic in array) {
            
            
            [self.dataArray addObject:dic];
            for (NSDictionary * aryDic in self.array)
            {
                    if ([aryDic[@"member_id"] isEqual:dic[@"member_id"]])
                    {
                        [self.dataArray removeObject:dic];
                    }
            }
        }
        [self.tableV reloadData];
    }] ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [[NSMutableArray alloc]init];
    self.addArray = [[NSMutableArray alloc]init];
    self.delArray = [[NSMutableArray alloc]init];
    self.title = @"我的盒子";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, MDSCREEN_HEIGHT - 100) style:(UITableViewStylePlain)];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    UIButton * newBoxBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, MDSCREEN_HEIGHT - 80, MDSCREEN_WIDTH - 200, 50)];
    newBoxBtn.backgroundColor = [UIColor colorWithHexStr:@"#6fac45"];
    [newBoxBtn setTitle:@"确定修改" forState:(UIControlStateNormal)];
    newBoxBtn.layer.cornerRadius = 25;
    newBoxBtn.layer.masksToBounds = true;
    [newBoxBtn addTarget:self action:@selector(addNewClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:newBoxBtn];
    [self getData];
}
- (void)addNewClick
{
    NSString * add = @"";
    for (int i=0; i<self.addArray.count; i++) {
        if (i==0) {
            add = [NSString stringWithFormat:@"%@",self.addArray[i]];
        }
        else
        {
        add = [NSString stringWithFormat:@"%@,%@",add,self.addArray[i]];
        }
    }
    NSString * del = @"";
    for (int i=0; i<self.delArray.count; i++) {
        if (i==0) {
            del = [NSString stringWithFormat:@"%@",self.delArray[i]];
        }
        else
        {
            del = [NSString stringWithFormat:@"%@,%@",del,self.delArray[i]];
        }
    }
    NSDictionary * dic = @{@"del_family_member_ids":del,@"add_family_member_ids":add,@"box_id":self.box_id};
    
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    [manger GET:@"https://h5.12590.com/cms/index.php/wxysapp/appInterface/reBindBox" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.description);
    }];
    
    
//    [MDNetDetail GETNet:MemberRebanding_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
//        NSLog(@"%@",posts);
//        [MBProgressHUD showInfoMessage:Meg];
//        if ([Meg isEqualToString:@"绑定成功"]) {
//            [self.navigationController popViewControllerAnimated:true];
//        }
//        
//    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 0;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, 100)];
        view.backgroundColor = [UIColor whiteColor];
        
        UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(30, 50, MDSCREEN_WIDTH - 60, 40)];
        titleLab.text = @"已添加家属（点击姓名选择要移除的家属）";
        titleLab.font = [UIFont systemFontOfSize:15];
        [view addSubview:titleLab];
        
        return view;
    }
    else if (section == 2)
    {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, 100)];
        view.backgroundColor = [UIColor colorWithHexStr:@"#ffffff"];
        
        UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(30, 50, MDSCREEN_WIDTH - 60, 40)];
        titleLab.text = @"可添加家属（点击姓名选择要添加的家属）";
        titleLab.font = [UIFont systemFontOfSize:15];
        [view addSubview:titleLab];
        return view;
    }
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, 100)];
    view.backgroundColor = [UIColor colorWithHexStr:@"#e0e0e0"];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 100;
    }
    else if (section == 1)
    {
        return 10;
    }
    else
    {
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellOne"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellOne"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        [self.backViewOne removeFromSuperview];
        self.backViewOne = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, 50+50 * self.array.count/2)];
        self.backViewOne.backgroundColor = [UIColor whiteColor];
        self.tableV.rowHeight = 50+50 * self.array.count/2;
        [cell.contentView addSubview:self.backViewOne];
        for (int i=0; i<self.array.count; i++) {
            NSDictionary * dic = self.array[i];
            BandingBtn * bandIngBtn = [[BandingBtn alloc]initWithFrame:CGRectMake(40 + (MDSCREEN_WIDTH - 320 + 120)*(i%2), 10 + (50)*(i/2), 120, 45)];
            bandIngBtn.nameLab.text = dic[@"name"];
            bandIngBtn.tag = 100+i;
            bandIngBtn.backgroundColor = [UIColor colorWithHexStr:@"#fbe9cc"];
            bandIngBtn.nameLab.textColor = [UIColor colorWithHexStr:@"#dc7a44"];
            bandIngBtn.selected = YES;
            bandIngBtn.imageV.image = [UIImage imageNamed:@"gou"];
            [bandIngBtn addTarget:self action:@selector(deleteClick:) forControlEvents:(UIControlEventTouchUpInside)];            [self.backViewOne addSubview:bandIngBtn];
        }
        return cell;
    }
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellTwo"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellTwo"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    [self.backView removeFromSuperview];
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, 50+50 * self.dataArray.count/2)];
    self.backView.backgroundColor = [UIColor whiteColor];
    self.tableV.rowHeight = 50+50 * self.dataArray.count/2;
    [cell.contentView addSubview:self.backView];
    for (int i=0; i<self.dataArray.count; i++) {
        BandingBtn * bandIngBtn = [[BandingBtn alloc]initWithFrame:CGRectMake(40 + (MDSCREEN_WIDTH - 320 + 120)*(i%2), 10 + (50)*(i/2), 120, 45)];
        NSDictionary * dic = self.dataArray[i];
        bandIngBtn.nameLab.text = dic[@"name"];
        bandIngBtn.backgroundColor = [UIColor colorWithHexStr:@"#f3f3f3"];
        bandIngBtn.nameLab.textColor = [UIColor colorWithHexStr:@"#646464"];
        bandIngBtn.tag = 1000+i;
        [bandIngBtn addTarget:self action:@selector(chiose:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.backView addSubview:bandIngBtn];
    }
    return cell;
}
- (void)deleteClick:(BandingBtn *)btn{
     btn.selected = !btn.selected;
    if (btn.selected == NO) {
        btn.backgroundColor = [UIColor colorWithHexStr:@"#f3f3f3"];
        btn.nameLab.textColor = [UIColor colorWithHexStr:@"#646464"];
        NSDictionary * dic = self.array[btn.tag - 100];
        [self.delArray addObject:[NSString stringWithFormat:@"%@",dic[@"member_id"]]];
         btn.imageV.image = [UIImage imageNamed:@""];
    }
    else
    {
        btn.backgroundColor = [UIColor colorWithHexStr:@"#fbe9cc"];
        btn.nameLab.textColor = [UIColor colorWithHexStr:@"#dc7a44"];
        NSDictionary * dic = self.array[btn.tag - 100];
        [self.delArray removeObject:[NSString stringWithFormat:@"%@",dic[@"member_id"]]];
         btn.imageV.image = [UIImage imageNamed:@"gou"];
    }
}
- (void)chiose:(BandingBtn *)btn{
     btn.selected = !btn.selected;
    if (btn.selected == YES) {
        btn.backgroundColor = [UIColor colorWithHexStr:@"#fbe9cc"];
        btn.nameLab.textColor = [UIColor colorWithHexStr:@"#dc7a44"];
        btn.imageV.image = [UIImage imageNamed:@"gou"];
        NSDictionary * dic = self.dataArray[btn.tag - 1000];
        [self.addArray addObject:[NSString stringWithFormat:@"%@",dic[@"member_id"]]];
    }
    else
    {
        btn.backgroundColor = [UIColor colorWithHexStr:@"#f3f3f3"];
        btn.nameLab.textColor = [UIColor colorWithHexStr:@"#646464"];
        btn.imageV.image = [UIImage imageNamed:@""];
        NSDictionary * dic = self.dataArray[btn.tag - 1000];
        [self.addArray removeObject:[NSString stringWithFormat:@"%@",dic[@"member_id"]]];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
